<?php

namespace app\api\controller\wanlshop;

use app\common\controller\Api;
use app\index\model\wanlshop\Shopnews;
use fast\Tree;
use think\Db;

/**
 * WanlShop店铺接口
 */
class Shop extends Api
{
    protected $noNeedLogin = ['getShopInfo', 'get_hot_shop','getNearbyShopList','getShopNewsList','getShopNewsInfo'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('app\api\model\wanlshop\Shop');
    }

    /**
     * 一次性获取店铺相关数据 1.0.8升级
     *
     * @ApiSummary  (WanlShop 一次性获取店铺相关数据)
     * @ApiMethod   (GET)
     *
     * @param string $id 页面ID
     */
    public function getShopInfo($id = null)
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        // 获取店铺信息
        $row = $this->model->get($id);
        if (!$row) {
            $this->error(__('未找到此商家'));
        }
        // 获取商家类目
        $tree = Tree::instance();
        $tree->init(
            model('app\api\model\wanlshop\ShopSort')
                ->where(['shop_id' => $row['id']])
                ->field('id, pid, name, image')
                ->order('weigh asc')
                ->select()
        );
        $row['category'] = $tree->getTreeArray(0);
        // 查看是否被关注
        $row['isFollow'] = model('app\api\model\wanlshop\find\Follow')
            ->where([
                'user_no' => $row['find_user']['user_no'],
                'user_id' => $this->auth->id
            ])
            ->count();
        $row['isLive'] = model('app\api\model\wanlshop\Live')
            ->where(['shop_id' => $row['id'], 'state' => 1])
            ->field('id')
            ->find();
        // 获取类目样式配置
        $shopConfig = model('app\api\model\wanlshop\ShopConfig')
            ->where(['shop_id' => $row['id']])
            ->find();
        $row['categoryStyle'] = (int)$shopConfig['category_style'];
        // 获取商家自定义页面
        $row['page'] = model('app\api\model\wanlshop\Page')
            ->where([
                'shop_id' => $row['id'],
                'type' => 'shop'
            ])
            ->field('id, name, page, item')
            ->find();
        $this->success('返回成功', $row);
    }

    /**
     * 获取金刚区前7个店铺
     */

    public function get_hot_shop()
    {
        $list = Db::name('wanlshop_shop')->field('id, avatar,shopname')
            ->order('weigh', 'desc')
            ->select();
        $this->success("ok", $list);
    }


    /**
	 * 获取商家列表

	 *
	 */
	public function getShopList()
	{
		//设置过滤方法
		$this->request->filter(['strip_tags']);

         $list = Db::name('wanlshop_shop')->field('id, avatar,shopname,isself,state,city,score_describe, score_service, score_deliver, (score_describe + score_service + score_deliver) AS total_score')
            ->order('total_score', 'desc')
            ->paginate();;

        $this->success("ok", $list);
		$this->success('返回成功', $list);
	}

    /**
     * 获取附近商家列表
     *
     */
    public function getNearbyShopList()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $lng = $this->request->post('lng',0);
        $lat = $this->request->post('lat',0);
        $km = $this->request->post('km',100);
        $where=[];
        if($lng!=0 && $lat!=0){
            $array  = return_square_point($lng, $lat,$km);
            $where['lat']  = array(array('EGT',$array['right-bottom']['lat']),array('ELT',$array['left-top']['lat']),'and');
            $where['lng'] = array(array('EGT',$array['left-top']['lng']),array('ELT',$array['right-bottom']['lng']),'and');
        }
        $shop_list = Db::name('wanlshop_shop')->field('id, lat,lng,pointaddr,avatar,shopname,isself,state,city,score_describe, score_service, score_deliver, (score_describe + score_service + score_deliver) AS total_score')
            ->where($where)
            ->order('total_score', 'desc')
            ->paginate();;
        $data=[];
        foreach ($shop_list as $k=>$item){
            $distance = bcmul(get_distance($lat,$lng,$item['lat'],$item['lng']),1000,0);  //计算两点间距离
            $item['distance']=bcdiv($distance,1000,2);
            $item['level_unit'] = 'km';
            $data[]=$item;
        }
        if(!empty($data)){
            array_multisort(array_column($data,'distance'),SORT_ASC,$data);
        }
        $shop_list=$data;
        $this->success('返回成功', $shop_list);
    }

    /**
     * 获取商家商家资讯列表
     *
     */
    public function getShopNewsList($id = null)
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        // 获取店铺信息
//        $row = $this->model->get($id);
        $limit = $this->request->post('limit',10);
        $id = $this->request->post('shop_id');
        if (!$id) {
            $this->error(__('未找到此商家'));
        }
        $list = (new Shopnews())->where(['shop_id'=>$id,'status'=>1])
            ->paginate($limit);;
        $goods_data=[];
        if(!is_null($list)){
            $data=$list->toArray();
            $goods_data=$data['data'];
        }
        foreach ($goods_data as $k=>$v){
            $v['img_path']=$this->request->domain().$v['img'];
            $goods_data[$k]=$v;
        }
        $data['data']=$goods_data;
        $this->success("ok", $data);
    }

    /**
     * 获取商家商家资讯详情
     *
     */
    public function getShopNewsInfo()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $id = $this->request->post('id');

        // 获取店铺信息
        $row = (new Shopnews())->get($id);
        if (!$row) {
            $this->error(__('未找到此资讯'));
        }
        $row->img=$this->request->domain().$row->img;
        $this->success("ok", $row);
    }


}